// pages/vip/index.js
const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')
const dateUtil = require('../../utils/date.js')
import Toast from 'tdesign-miniprogram/toast/index';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    userList: [],
    vipOptions:[],
    interval: null,
    vipVisible:false,
    backTopVisible: false,
    current_no: 0,
    isLastPage: false,
    page_size: 10,
    baseRefresh: {
      value: false,
    },
    loadingProps: {
      size: '50rpx',
    },
  },
  onPullDownRefresh() {
    
    this.setData({
      current_no:0,
      orderList:[],
      isLastPage:false
    })
    this.queryUserList(() => {
      this.setData({
        'baseRefresh.value': false
      })
    });

  },
  onVIPPicker(e){
    const {index}= e.currentTarget.dataset;
    var userList=this.data.userList;
    userList[index].vipVisible=true;
   this.setData({
    userList
   })
  },
  queryVIPList() {
    WXAPI.queryVIPList().then(res => {
      var vipList = res;
      var vipOptions=[];
      vipList.forEach(item=>{
        vipOptions.push({
          "value":item.id,
          "label":item.name
        })
      })
      this.setData({
        vipOptions
      });
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  queryUserList(callback) {
    if (this.data.isLastPage) {
      wx.showToast({
        title: '没有更多的数据'
      })
      return;
    }
    this.setData({
      isLastPage: false,
    })
    var { current_no, page_size } = this.data;
    var param = {
      current_no,
      page_size
    }


    WXAPI.queryUserList(param).then(res => {
      var userList = res;
      userList.forEach(item=>{
        userList.vipVisible=false;
      })
      this.setData({
        userList
      });
      if(callback)
      callback()
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  onPickerChange(e) {
    const { value, label } = e.detail;
    const {index}= e.currentTarget.dataset;
    var userList=this.data.userList;
    userList[index].vip_id=value[0]
    userList[index].vip_name=label[0]
    userList[index].vipVisible=false
    console.log('picker confirm:', e.detail);
    this.setData({
      userList
    });
  },
  inputeidt: function (e) {
    let dataset = e.currentTarget.dataset
    this.data.userList[dataset.index][dataset.name] = e.detail.value
  },
  //触底刷新
  loadMoreData() {
    if (this.data.isLastPage) {
      return;
    }
    var { current_no, page_size } = this.data;

    current_no = current_no + page_size;
    var param = {
      current_no,
      page_size
    }
    WXAPI.queryUserList(param).then(res=> {

      var userList = res;
     
      if (userList.length > 0) {
        userList.forEach(item=>{
          userList.vipVisible=false;
        })
        var newList = this.data.userList.concat(userList);
        this.setData({
          userList:newList,
          current_no
        });        
      } else {
        this.setData({
          isLastPage: true,
        });
      }
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  refreshUser(e){
    const {index}= e.currentTarget.dataset;
    const data=this.data.userList[index]
    var user_id=data.id;
    var param={
      recharge_balance:data.recharge_balance,
      vip_id:data.vip_id,
      is_vip:data.vip_id!=undefined,
      vip_name:data.vip_name,
      nick_name:data.nick_name,
      phone_no:data.phone_no
    }
    debugger
    WXAPI.updateUser(user_id,param).then(res=> {

      wx.showToast({
        "title":res.code==200?"更新成功":res.msg
      });  
      return;
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    var userInfo = wx.getStorageSync("userInfo");
    // userInfo.vip_expire_time = dateUtil.toDateShort(userInfo.vip_expire_time)
    this.setData({
      userInfo
    })
    this.queryUserList();
    this.queryVIPList();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})